package com.colin.jipay.utils;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

/**
 * 无障碍工具类
 */
public class AccessibilityUtils {
    private static final String TAG = "AccessibilityUtils";

    private static long lastRefreshTime = 0;

    /**
     * 该辅助功能开关是否打开了
     *
     * @param accessibilityServiceName：完整的指定辅助服务名字
     * @param context：上下文
     * @return
     */
    public static boolean isAccessibilitySettingsOn(String accessibilityServiceName, Context context) {
        int accessibilityEnable = 0;
        String serviceName = context.getPackageName() + "/" + accessibilityServiceName;
        try {
            accessibilityEnable = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0);
        } catch (Exception e) {
            Log.e(TAG, "get accessibility enable failed, the err:" + e.getMessage());
        }
        if (accessibilityEnable == 1) {
            TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
            String settingValue = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();
                    if (accessibilityService.equalsIgnoreCase(serviceName)) {
                        Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
                        return true;
                    }
                }
            }
        } else {
            Log.d(TAG, "Accessibility service disable");
        }
        return false;
    }

    /**
     * 跳转到系统设置页面开启辅助功能
     *
     * @param accessibilityServiceName：指定辅助服务名字
     * @param context：上下文
     */
    public static void openAccessibility(String accessibilityServiceName, Context context) {
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        startActivity(intent);
    }

    /**
     * 启动无障碍设置
     */
    public static void startAccessibility(Context context) {
        boolean isAccessibilitySettingsOn = AccessibilityUtils.isAccessibilitySettingsOn("com.xposed.hook.pinduoduo.service.MyAccessibilityService", context);
        if (!isAccessibilitySettingsOn) {
            ToastUtils.showShort("请先启动pinduoduo的无障碍功能");
            AccessibilityUtils.openAccessibility("MyAccessibilityService", context);
        }
    }

    /**
     * 是否是快速刷新了收银台界面的activity
     *
     * @return true:快速刷新了
     */
    public static boolean isFastRefreshPayActivity() {
        long nowTime = System.currentTimeMillis();
        LogUtils.i(nowTime + "", lastRefreshTime + "");
        if ((nowTime - lastRefreshTime) > 10000) {
            lastRefreshTime = nowTime;
            return false;
        }
        return true;
    }
}
